//
//  XKCertificationCell.m
//  xk100
//
//  Created by xiaokang on 2017/7/4.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "XKCertificationCell.h"

@interface XKCertificationCell () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *contentTextField;

@end
@implementation XKCertificationCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubviews];
    }
    return self;
}

#pragma mark -
- (void)initSubviews {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _titleLabel = [UILabel getLabWithText:@"----" textFont:Font_15 textColor:[UIColor darkGrayColor]];
    
    _contentTextField                  = [[UITextField alloc] init];
    _contentTextField.font             = Font(15);
    _contentTextField.textColor        = [UIColor blackColor];
    _contentTextField.placeholder      = @"----";
    _contentTextField.delegate         = self;
    _contentTextField.secureTextEntry  = NO;
    _contentTextField.clearButtonMode  = UITextFieldViewModeWhileEditing;
    _contentTextField.borderStyle      = UITextBorderStyleNone;
    [_contentTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    [self.contentView addSubview:_titleLabel];
    [self.contentView addSubview:_contentTextField];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).offset(20*Scale_W);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(68*Scale_W);
        make.height.mas_equalTo(50*Scale_W);
        make.bottom.mas_equalTo(self.contentView).priorityHigh();
    }];
    
    [self.contentTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel.mas_right).offset(10*Scale_W);
        make.centerY.height.mas_equalTo(_titleLabel);
        make.right.mas_equalTo(self.contentView).offset(-20*Scale_W);
    }];
}

#pragma mark -
- (void)textFieldDidChange:(UITextField *)sender {
    if (self.TextChangedBlock) {
        self.TextChangedBlock(_contentTextField.text);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = [NSString stringWithFormat:@"%@%@",textField.text, string];
    if (_userInfoType == UserInfoType_IDNumber) {
        return text.length > 16 ? NO : YES;
    }
    
    if (_userInfoType == UserInfoType_TradePwd || _userInfoType == UserInfoType_TradePwdConfirm) {
        return text.length > 6 ? NO : YES;
    }
    
    if (_userInfoType == UserInfoType_Gender) {
        return text.length > 3 ? NO : YES;
    }
    
    return YES;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLabel.text = title;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    _contentTextField.placeholder = placeholder;
}

- (void)setSecureTextEntry:(BOOL)secureTextEntry {
    _secureTextEntry = secureTextEntry;
    _contentTextField.secureTextEntry = secureTextEntry;
}

- (void)setUserInfoType:(UserInfoType)userInfoType {
    _userInfoType = userInfoType;
    if (userInfoType == UserInfoType_TradePwd || userInfoType == UserInfoType_TradePwdConfirm) {
        _contentTextField.keyboardType = UIKeyboardTypeNumberPad;
    } else if (userInfoType == UserInfoType_IDNumber) {
        _contentTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    } else {
        _contentTextField.keyboardType = UIKeyboardTypeDefault; 
    }
}

- (void)setText:(NSString *)text {
    _text = text;
    _contentTextField.text = text;
}

@end
